﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace JZ_UDI.CommonHelper
{
    public static class GetUdiCheckCodeHelper
    {
        /// <summary>
        /// 计算校验码
        /// </summary>
        /// <param name="projectCode">3位项目代码</param>
        /// <param name="vendorCode">9位厂商代码</param>
        public static int GetUdiCheckCode(string projectCode,string vendorCode)
        {
            string str = vendorCode + projectCode+"0";
            if (str.Length != 13)
            {
                throw new Exception("厂商代码与项目代码不足12位");
            }
            int index1 = int.Parse(str.Substring(str.Length - 1, 1));
            int index2 = int.Parse(str.Substring(str.Length - 2, 1));
            int index3 = int.Parse(str.Substring(str.Length - 3, 1));
            int index4 = int.Parse(str.Substring(str.Length - 4, 1));
            int index5 = int.Parse(str.Substring(str.Length - 5, 1));
            int index6 = int.Parse(str.Substring(str.Length - 6, 1));
            int index7 = int.Parse(str.Substring(str.Length - 7, 1));
            int index8 = int.Parse(str.Substring(str.Length - 8, 1));
            int index9= int.Parse(str.Substring(str.Length - 9, 1));
            int index10 = int.Parse(str.Substring(str.Length - 10, 1));
            int index11 = int.Parse(str.Substring(str.Length - 11, 1));
            int index12 = int.Parse(str.Substring(str.Length - 12, 1));
            int index13 = int.Parse(str.Substring(str.Length - 13, 1));
            int sumS = index2 + index4 + index6 + index8 + index10 + index12;
            int sum3 = sumS * 3;
            int sumD = index3 + index5 + index7 + index9 + index11 + index13;
            int sumH = sum3 + sumD;
            string sumStr = sumH.ToString();
            int gw=int.Parse(sumStr.Substring(sumStr.Length - 1, 1));
            if (gw == 0)
            {
                return 0;
            }
            else
            {
                return 10 - gw;
            }
        }

    }
}
